<template>
  <div>
    <!-- 导航 -->
    <van-nav-bar title="登录" class="page-nav-bar">
      <template #left>
        <van-icon name="cross" @click="$router.back()" />
      </template>
    </van-nav-bar>
    <!-- 表单 -->
    <van-form @submit="onSubmit" class="form" ref="loginForm">
      <van-field
        v-model="user.mobile"
        name="mobile"
        type="number"
        maxlength="11"
        placeholder="请输入手机号"
        :rules="userRules.mobile"
      >
        <template #left-icon>
          <i class="toutiao toutiao-shouji"></i>
        </template>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        type="number"
        maxlength="6"
        placeholder="请输入验证码"
        :rules="userRules.code"
      >
        <template #left-icon>
          <i class="toutiao toutiao-yanzhengma"></i>
        </template>
        <!-- 发送验证码按钮 -->
        <template #button>
          <!-- 倒计时 -->
          <van-count-down
            v-if="showCountDown"
            :time="1000 * 60"
            format="ss s后可重新发送"
            @finish="showCountDown = false"
          />
          <van-button
            v-else
            class="send-sms-btn"
            round
            size="small"
            native-type="button"
            @click="onSendyzm"
            >发送验证码
          </van-button>
        </template>
      </van-field>
      <div class="loginbtn">
        <!-- 登录按钮 -->
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <div class="box">
      手机号: 13911111111/18911111111
      <br />
      万能验证码: 246810
      <br />
      测试文章ID:1374429883790262272
    </div>
  </div>
</template>

<script>
import { login, sendCode } from '@/api/user.js'
// import { setItem } from '@/utils/storage.js'
export default {
  name: 'LoginIndex',
  data () {
    return {
      // 表单绑定值
      user: {
        mobile: '',
        code: ''
      },
      // 表单规则
      userRules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3456789]\d{9}$/, message: '手机号格式错误' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      // show
      showCountDown: false
    }
  },
  methods: {
    // 点击登录
    async onSubmit () {
      // try catch捕获异常
      try {
        const { data: res } = await login(this.user)
        console.log(res)
        // token保存在vuex
        this.$store.commit('setUser', res.data)
        this.$toast.success('登录成功')
        // back 不严谨
        this.$router.back()
      } catch (err) {
        if (err.response && err.response.status === 400) {
          this.$toast.fail('用户名或验证码有误')
        } else {
          this.$toast.fail('登录失败')
        }
      }
    },
    // 点击获取验证码
    async onSendyzm () {
      // 预校验
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        console.dir(err)
        return this.$toast.fail(err.message)
      }
      // 发送请求
      try {
        await sendCode(this.user.mobile)
        this.showCountDown = true
        this.$toast.success('验证码已发送')
      } catch (err) {
        // 发送失败,关闭倒计时
        this.showCountDown = false
        console.dir(err)
        if (err.response && err.response.status === 429) {
          return this.$toast.fail('请求频繁,请一分钟后再试')
        } else if (err.response && err.response.status === 404) {
          return this.$toast.fail('发送失败,请稍后再试')
        }
      }
    }
  }
}
</script>

<style scoped lang='less'>
// 字体图标
.toutiao {
  font-size: 34px;
  padding-right: 15px;
  vertical-align: middle;
}
// 发送验证码按钮
.send-sms-btn {
  width: 170px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
  vertical-align: middle;
}
// 登陆按钮
.loginbtn {
  padding: 53px 33px;
  .van-button {
    background-color: #6db4fb;
    border-radius: 10px;
    border: none;
  }
}
.box {
  text-align: center;
  font-size: 30px;
  color: #ccc;
}
/deep/ .van-field__error-message,
/deep/ .van-field--error .van-field__control,
/deep/ .van-field--error .van-field__control::placeholder {
  color: #ff5b5b !important;
}
/deep/ .van-field__error-message {
  font-size: 20px;
}
.van-count-down {
  font-size: 20px;
  color: #ccc;
}
</style>
